package com.bacon.gmall.web.controller;

import com.bacon.gmall.common.result.Result;
import com.bacon.gmall.model.product.BaseCategoryView;
import com.bacon.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author： bacon、
 * @version： 1.0
 * @create： 2020-12-23 21:16
 * @description:
 */
@Controller
public class IndexController {

    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private TemplateEngine templateEngine;

    @GetMapping("/")
    public String index(Model model) {
        //使用静态页面后，不要实时查询数据，只需每天执行一次，将文件放入指定位置即可
        List<Map<String, Object>> resultMapList = getData();
        model.addAttribute("list", resultMapList);
        return "index/index";
    }

    public List<Map<String, Object>> getData() {

        //存放数据
        List<Map<String, Object>> resultMapList = new ArrayList<>();
        List<BaseCategoryView> baseCategoryViewList = productFeignClient.getBaseCategoryViewList();

        //根据分类Id1进行分组
        Map<Long, List<BaseCategoryView>> category1ViewMap = baseCategoryViewList.stream().
                collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));

        int index = 0;
        for (Map.Entry<Long, List<BaseCategoryView>> category1ViewEntry : category1ViewMap.entrySet()) {
            List<BaseCategoryView> category1ViewEntryValue = category1ViewEntry.getValue();
            Map<String, Object> category1Map = new HashMap<>();

            //存放一级分类数据
            category1Map.put("index", ++index);
            category1Map.put("categoryId", category1ViewEntry.getKey());
            category1Map.put("categoryName", category1ViewEntryValue.
                    get(0).getCategory1Name());

            //根据分类id2再次进行分类
            Map<Long, List<BaseCategoryView>> category2ViewMap = category1ViewEntryValue.
                    stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));

            //使用队列装起来后，在放入到map中
            List<Map<String, Object>> category2IdMapList = new ArrayList<>();
            for (Map.Entry<Long, List<BaseCategoryView>> category2ViewEntry : category2ViewMap.entrySet()) {
                List<BaseCategoryView> category2ViewEntryValue = category2ViewEntry.getValue();
                Map<String, Object> category2Map = new HashMap<>();

                category2Map.put("categoryId", category2ViewEntry.getKey());
                category2Map.put("categoryName", category2ViewEntryValue.
                        get(0).getCategory2Name());

                //偷梁换柱，将BaseCategoryView换成map，使用stream流可以实现
                List<Map> category3Map = category2ViewEntryValue.stream().map((baseCategoryView) -> {
                    Map map = new HashMap<>();
                    map.put("categoryId", baseCategoryView.getCategory3Id());
                    map.put("categoryName", baseCategoryView.getCategory3Name());
                    return map;
                }).collect(Collectors.toList());

                //放入二级分类数据
                category2Map.put("categoryChild", category3Map);
                category2IdMapList.add(category2Map);
            }

            //放入数据
            category1Map.put("categoryChild", category2IdMapList);
            resultMapList.add(category1Map);

        }

        return resultMapList;
    }

    //使用静态化技术
    @GetMapping("/createHtml")
    @ResponseBody
    public Result createHtml() throws IOException {

        /**
         * 1、模板+数据 = 输出
         * 2、模板 页面 （标签： Thymeleaf）
         * 3、数据为一二三级分类
         * 4、输出 D:\temp
         */

        Context context = new Context();
        context.setVariable("list", getData());
        File file;
        Writer writer = new PrintWriter("D:\\temp\\index.html", "utf-8");

        templateEngine.process("index/index", context, writer);
        return Result.ok();
    }

}
